package br.com.gbvbahia.commons.Transacao;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import br.com.gbvbahia.commons.utils.AndroidUtils;

/**
 * Classe para controlar a thread
 * 
 * @author ricardo
 * 
 */
public class TransacaoFragmentTask extends AsyncTask<Void, Void, Boolean> {
	private ProgressDialog progresso;
	private final Context context;
	private final Fragment fragment;
	private final Transacao transacao;
	private Exception exceptionErro;
	private int appName;
	private int aguardeMsg;

	public TransacaoFragmentTask(Fragment fragment, Transacao transacao,
			int appName, int aguardeMsg) {
		this.context = fragment.getActivity();
		this.fragment = fragment;
		this.transacao = transacao;
		this.appName = appName;
		this.aguardeMsg = aguardeMsg;
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		try {
			// Exibe o ProgressBar antes de iniciar a trana��o
			showProgress();
		} catch (Exception e) {
			Log.e(Transacao.TAG, e.getMessage(), e);
		}
	}

	@Override
	protected Boolean doInBackground(Void... params) {
		try {
			transacao.executar();
		} catch (Exception e) {
			Log.e(Transacao.TAG, e.getMessage(), e);
			// Salva o erro e retorna false
			this.exceptionErro = e;
			return false;
		} finally {
			try {
				// Ao final da transa��o desliga o ProgressBar
				// Sincroniza com a thread de interface
				Activity ac = fragment.getActivity();
				if (ac != null) {
					ac.runOnUiThread(new Runnable() {
						@Override
						public void run() {
							hideProgress();
						}
					});
				}
			} catch (Exception e) {
				Log.e(Transacao.TAG, e.getMessage(), e);
			}
		}
		// OK
		return true;
	}

	@Override
	protected void onPostExecute(Boolean ok) {
		if (ok) {
			// Transa��o executou com sucesso
			transacao.atualizarView();
		} else {
			// Erro
			AndroidUtils.alertDialog(context,
					"Erro: " + exceptionErro.getMessage(), appName);
		}
	}

	// Exibe o ProgressBar
	private void showProgress() {
		if (aguardeMsg != 0) 
		try {
			progresso = ProgressDialog.show(context, "",
					context.getString(aguardeMsg));
		} catch (Throwable e) {
			Log.e(Transacao.TAG, e.getMessage(), e);
		}
	}

	// Desliga o ProgressBar
	private void hideProgress() {
		try {
			if (progresso != null) {
				progresso.dismiss();
			}
		} catch (Throwable e) {
			Log.e(Transacao.TAG, e.getMessage(), e);
		}
	}

	public void abrirProgress() {
		if (aguardeMsg != 0) {
			try {
				progresso = ProgressDialog.show(context, "",
						context.getString(aguardeMsg));
			} catch (Throwable e) {
				Log.e(Transacao.TAG, e.getMessage(), e);
			}
		}
	}

	public void fecharProgress() {
		try {
			if (progresso != null) {
				progresso.dismiss();
			}
		} catch (Throwable e) {
			Log.e(Transacao.TAG, e.getMessage(), e);
		}
	}
}
